package com.sunday.haowushop.ui.order;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.SpannalbeStringUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.haowushop.R;
import com.sunday.haowushop.common.BaseApp;
import com.sunday.haowushop.entity.OpenShopOrder;
import com.sunday.haowushop.http.ApiClient;
import com.sunday.haowushop.ui.WebViewActivity;

import java.math.RoundingMode;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/8/3.
 * <p>
 * 开店订单
 */

public class OpenShopOrderActivity extends BaseActivity {


    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.order_time)
    TextView orderTime;
    @Bind(R.id.tvStatus)
    TextView tvStatus;
    @Bind(R.id.product_image)
    ImageView productImage;
    @Bind(R.id.product_name)
    TextView productName;
    @Bind(R.id.product_price)
    TextView productPrice;
    @Bind(R.id.valid_time)
    TextView validTime;
    @Bind(R.id.product_spec)
    TextView productSpec;
    @Bind(R.id.product_num)
    TextView productNum;
    @Bind(R.id.pro_layout)
    RelativeLayout proLayout;
    @Bind(R.id.total_money)
    TextView totalMoney;
    @Bind(R.id.rl_total_layout)
    RelativeLayout rlTotalLayout;
    @Bind(R.id.order_btn2)
    Button orderBtn2;
    @Bind(R.id.order_btn1)
    Button orderBtn1;
    @Bind(R.id.btn_layout)
    RelativeLayout btnLayout;
    @Bind(R.id.item_view)
    LinearLayout itemView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_open_shop_order);
        ButterKnife.bind(this);
        titleView.setText("开店订单");
        getData();

    }

    private OpenShopOrder order;

    private void getData() {
        long memberId = BaseApp.getInstance().getMember().getId();
        Call<ResultDO<OpenShopOrder>> call = ApiClient.getApiAdapter().getShopOrder(memberId);
        call.enqueue(new Callback<ResultDO<OpenShopOrder>>() {
            @Override
            public void onResponse(Call<ResultDO<OpenShopOrder>> call, Response<ResultDO<OpenShopOrder>> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    order = response.body().getResult();
                    updateView(order);
                }else {
                    ToastUtils.showToast(mContext,response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<OpenShopOrder>> call, Throwable t) {
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }

    private void updateView(OpenShopOrder shopOrder) {
        if (shopOrder == null) {
            return;
        }
        orderTime.setText(shopOrder.getCreateTime());
        if (shopOrder.getStatus() == 1) {
            //待发货
            btnLayout.setVisibility(View.GONE);
            tvStatus.setText("待发货");
        } else if (shopOrder.getStatus() == 4) {
            //已完成
            btnLayout.setVisibility(View.GONE);
            tvStatus.setText("已完成");
        } else {
            btnLayout.setVisibility(View.VISIBLE);
            tvStatus.setText("待收货");
        }
        validTime.setText("有效期" + shopOrder.getYear());
        productSpec.setText("规格:" + shopOrder.getElements());
        productName.setText(shopOrder.getTitle());
        if (!TextUtils.isEmpty(shopOrder.getImage()))
            Glide.with(mContext)
                    .load(shopOrder.getImage())
                    .into(productImage);
        productPrice.setText(String.format("¥%s", shopOrder.getTotalFee().setScale(2, RoundingMode.HALF_UP)));
        totalMoney.setText(String.format("¥%s", shopOrder.getTotalFee().setScale(2, RoundingMode.HALF_UP)));
        totalMoney.append(SpannalbeStringUtils.setTextColor("(运费0.00)", ContextCompat.getColor(mContext,R.color.black_6)));

    }

    @OnClick({R.id.order_btn1, R.id.order_btn2,R.id.pro_layout})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.order_btn2:
                intent = new Intent(mContext, WebViewActivity.class);
                String url = String.format("https://m.kuaidi100.com/index_all.html?type=%1$s&postid=%2$s", order.getExpressEng(), order.getExpressNo());
                intent.putExtra("url", url);
                intent.putExtra("title", "物流查询");
                startActivity(intent);
                break;
            case R.id.order_btn1:
                //确认收货
                confirmRecieve();
                break;
            case R.id.pro_layout:
                intent=new Intent(mContext,ShopGiftOrderDetailActivity.class);
                intent.putExtra("shopOrder",order);
                startActivity(intent);
                break;
        }
    }

    private void confirmRecieve() {
        showLoadingDialog(0);
        Call<ResultDO> call = ApiClient.getApiAdapter().confirmRecieveShopOrder(order.getId());
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if (isFinish) {
                    return;
                }
                dissMissDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext, "确认成功");
                    getData();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }


}
